//package com.utilcase.config;
//
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.amqp.core.*;
//import org.springframework.amqp.rabbit.connection.ConnectionFactory;
//import org.springframework.amqp.rabbit.core.RabbitTemplate;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.boot.context.properties.EnableConfigurationProperties;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//
//import java.util.List;
//import java.util.Map;
//import java.util.stream.Collectors;
//
//@Configuration
//@EnableConfigurationProperties(RabbitMQConfigProperties.class)
//@Slf4j
//public class RabbitMQConfig {
//    @Autowired
//    private RabbitMQConfigProperties rabbitMQConfigProperties;
//
//    @Bean
//    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory){
//        log.info("初始化rabbitTemplate");
//        RabbitTemplate rabbitTemplate = new RabbitTemplate();
//        rabbitTemplate.setConnectionFactory(connectionFactory);
//        rabbitTemplate.setMandatory(true);
//        //开启Mandatory,才能触发回调函数
//        rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> {
//            log.info("消息发送成功:correlationData({}),ack({}),cause({})", correlationData, ack, cause);
//        });
//        rabbitTemplate.setReturnsCallback(returnedMessage -> {
//            log.info("消息丢失:exchange({}),route({}),replyCode({}),replyText({}),body({})", returnedMessage.getExchange(),
//                    returnedMessage.getRoutingKey(), returnedMessage.getReplyCode(), returnedMessage.getReplyText(),
//                    new String(returnedMessage.getMessage().getBody()));
//        });
//        return rabbitTemplate;
//    }
//
//    @Bean
//    public List<Queue> queues() {
//        log.info("初始化队列");
//        return rabbitMQConfigProperties.getQueues().stream()
//                .map(p -> new Queue(p.getName(), p.isDurable(), p.isExclusive(), p.isAutoDelete(), p.getArguments()))
//                .collect(Collectors.toList());
//    }
//
//    @Bean
//    public List<Exchange> exchanges() {
//        log.info("初始化交换机");
//        return rabbitMQConfigProperties.getExchanges().stream()
//                .map(p -> {
//                    Exchange exchange = null;
//                    switch (p.getType()) {
//                        case DIRECT:
//                            exchange = new DirectExchange(p.getName());
//                            break;
//                        case TOPIC:
//                            exchange = new TopicExchange(p.getName());
//                            break;
//                        case HEADERS:
//                            exchange = new HeadersExchange(p.getName());
//                            break;
//                        case FANOUT:
//                            exchange = new FanoutExchange(p.getName());
//                            break;
//                        case CUSTOM:
//                            exchange = new CustomExchange(p.getName(), p.getCustomType(), true, false, p.getArguments());
//                            break;
//                        default:
//                            break;
//                    }
//                    return exchange;
//                })
//                .collect(Collectors.toList());
//    }
//
//    @Bean
//    public List<Binding> bindings() {
//        log.info("初始化绑定关系");
//        Map<String, Queue> queueMap = queues().stream().collect(Collectors.toMap(Queue::getName, queue -> queue));
//        Map<String, Exchange> exchangeMap = exchanges().stream()
//                .filter(exchange -> exchange instanceof DirectExchange)
//                .collect(Collectors.toMap(Exchange::getName, exchange -> exchange));
//        return rabbitMQConfigProperties.getBindings().stream()
//                .map(p -> BindingBuilder.bind(queueMap.get(p.getSource()))
//                        .to((DirectExchange) exchangeMap.get(p.getDestination()))
//                        .with(p.getRoutingKey()))
//                .collect(Collectors.toList());
//    }
//}